# -*- coding:utf-8 -*-
import time

from com import MyDevice, LogUtil, TimeUtils
from flash_tool.flash import FlashTools
from fluency_test import FpsPath, SlideTask, FpsDataUtil, GfxFrame

# 服务器地址
server_dir = r'\\172.16.1.230\Tiffany\3.dailybuild_release\11.MEIOS_T9_PVT_DailyBuild_8.1'
# 设备序列号
T9_num = 'cf4fca46'  # 'e0abc420'#'cf4fca46'# 'c4adc20c'  # ' 'VINLHD2781400217''c5c3c2e2'  # 'c5c3c2e2'  'c4b5c29f' cf47caad c4adc20c
# xls html等文件的存放路径
is_debug = False
mpath = FpsPath(is_debug)


def exe_test(msg='', day_delta=1):
    LogUtil.i(msg)
    # 刷机
    my_dev = MyDevice(T9_num)
    new_rom = FlashTools.get_daily_zip(server_dir, unzip_dir='./daily_rom/', day_delta=day_delta)
    if new_rom is None:
        LogUtil.e('版本异常,当天测试中断！')
        return
    flash_ok = FlashTools.flash_by_py(T9_num, new_rom, save_userdata=True)
    if not flash_ok:
        LogUtil.e('刷机失败！当天测试中断！')
        return
    # 等待开机完毕
    my_dev.wait_device_ready()
    time.sleep(10)

    # 开始测试,dev要重新初始化，版本号才会更新
    my_dev = MyDevice(T9_num)
    slide_task = SlideTask(my_dev, mpath)
    slide_task.start()
    slide_task.join()

    # 绘制曲线图
    FpsDataUtil.parse_histroy_data(mpath)


def timer_fun(hour, min):
    exe_time = str(hour) + ':' + str(min)
    import schedule
    schedule.every().day.at(exe_time).do(exe_test, '开始流畅性自动化测试！')
    while True:
        schedule.run_pending()
        time.sleep(10)
        LogUtil.i(TimeUtils.format_current_time_ms() + ' wating...')


def cllect_histroy_data(first_version=7):
    for i in range(1, first_version):
        day = first_version - i
        exe_test(day_delta=day)


if __name__ == '__main__':
    timer_fun(9, 00)
    # FpsDataUtil.parse_histroy_data(mpath)
